// const AV = require('../../utils/av-weapp.js')
const pageInterceptor = require('../../components/pageInterceptor').pageInterceptor;
const api = require('../../api/api');
const request = require('../../components/request');

// Page({
pageInterceptor({
  data: {
    /**
     * 轮播图
     */
    banner: [],
    /**
     * 商品
     */
    goods: [],
    /**
     * 轮播图高度
     */
    bannerHeight: Math.ceil(290.0 / 750.0 * getApp().screenWidth)
  },
  onLoad: function (options) {
    console.log(`调用home.js文件的onLoad方法`);
    // 加载轮播图
    this.loadBanner();
    // 加载热门商品
    this.loadMainGoods();
    // this.getInviteCode(options);
  },
  onTabItemTap: function () {
    console.log(`调用home.js文件的onTabItemTap方法`);
  },
  getInviteCode: function (options) {
    if (options.uid != undefined) {
      wx.showToast({
        title: '来自用户:' + options.uid + '的分享',
        icon: 'success',
        duration: 2000
      })
    }
  },
  /**
   * 加载轮播图
   */
  loadBanner: function () {
    let that = this;
    request.get(api.data.prefix + api.data.url.banner.findAll, null, {
      'content-type': 'application/json',
    }).action((res) => {
      console.log(`接口${api.data.url.banner.findAll}：${JSON.stringify(res)}`);
      var banner = [];
      for (var i = 0; i < res.data.result.length; i++) {
        banner.push(res.data.result[i]);
      }
      that.setData({
        banner: banner
      });
    }).catch(err => {
      console.error(err);
    });
  },
  /**
   * 加载热门商品
   */
  loadMainGoods: function () {
    let that = this;
    request.get(api.data.prefix + api.data.url.goods.findHotGoods, null, {
      'content-type': 'application/json',
    }).action((res) => {
      console.log(`接口${api.data.url.goods.findHotGoods}：${JSON.stringify(res)}`);
      that.setData({
        goods: res.data.result
      });
    }).catch(err => {
      console.error(err);
    });
  },
  /**
   * 跳转到商品详细信息页面
   * @param {*} e 
   */
  showDetail: function (e) {
    let index = e.currentTarget.dataset.index;
    let goodsId = this.data.goods[index-1].id;
    wx.navigateTo({
      url: "../goods/detail/detail?objectId=" + goodsId
    });
  },
  /**
   * 跳转到分类页面
   */
  showCategories: function () {
    // wx.navigateTo({
    // 	url: "../category/category"
    // });
    wx.switchTab({
      url: "../category/category"
    });
  },
  /**
   * 跳转到订单列表页面
   */
  showOrders: function () {
    wx.navigateTo({
      url: "../order/list/list?status=1"
    });
  },
  onShareAppMessage: function () {
    return {
      title: '灵动开源电商系统',
      desc: '一个基于LeanCloud开发的开源电商系统',
      path: '/pages/index/index?uid=4719784'
    }
  },
  /**
   * 跳转到商品详细信息页面
   */
  showGoods: function (e) {
    let id = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: `../goods/detail/detail?objectId=${id}`
    });
  }
  // })
}, true)